package com.xcity.game.conquests;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Build;
import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Employee.State;
import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.common.Cause;
import com.xcity.game.employee.EmployeeQuality;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.template.EmployeeProduceMoneyByQualityV2Template;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.game.time.TimeUpdater;

/**
 * 占领的建筑信息
 * @author yang.li
 *
 */
public class Conquest {

	@JSONField(name = "0")
	protected long ownerId; // 该建筑原主人id
	@JSONField(name = "1")
	protected int id; // 建筑id
	@JSONField(name = "2")
	protected long[] employees; // 驻守我方员工
	@JSONField(name = "3")
	protected long lastStartMakeMoneyTime;//上一次开始产钱的时间 -1不产钱
//	@JSONField(name = "4")
//	protected long lastTakeMoneyTime;//上一次取钱的时间 用于判断能不能拿钱
//	@JSONField(name = "4")
//	protected long lastCapureTime;//上一次攻占时间,用于判断过没过期
	
	protected Conquests vm;
	
	public Conquest() {}
	
	@Deprecated
	public Conquest(Conquests vm, Build build, List<Employee> defenders) {
		Player owner = build.getManager().getOwner();
		this.ownerId = owner.getId();
		this.id = build.getId();
		this.vm = vm;
		this.employees = new long[defenders.size()];
		for (int i = 0; i < defenders.size(); i++) {
			Employee defender = defenders.get(i);
			defender.setState(State.CAPTURE, true);
			this.employees[i] = defender.getId();
		}
	}
	
	public Conquest(Conquests vm, BuildV2 build, List<Employee> defenders) {
		Player owner = build.getVm().getOwner();
		this.ownerId = owner.getId();
		this.id = build.getId();
		this.vm = vm;
		this.employees = new long[defenders.size()];
		for (int i = 0; i < defenders.size(); i++) {
			Employee defender = defenders.get(i);
			defender.setState(State.CAPTURE, true);
			this.employees[i] = defender.getId();
		}
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long ownerId) {
		this.ownerId = ownerId;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public long[] getEmployees() {
		return employees;
	}

	public void setEmployees(long[] employees) {
		this.employees = employees;
	}

	public long getLastStartMakeMoneyTime() {
		return lastStartMakeMoneyTime;
	}

	public void setLastStartMakeMoneyTime(long lastStartMakeMoneyTime) {
		this.lastStartMakeMoneyTime = lastStartMakeMoneyTime;
	}

//	public long getLastTakeMoneyTime() {
//		return lastTakeMoneyTime;
//	}
//
//	public void setLastTakeMoneyTime(long lastTakeMoneyTime) {
//		this.lastTakeMoneyTime = lastTakeMoneyTime;
//	}

	public Player owner() {
		return App.getApp().getServiceManager().get(PlayerCacheService.class).find(ownerId);
	}
	
//	public long getLastCapureTime() {
//		return lastCapureTime;
//	}
//
//	public void setLastCapureTime(long lastCapureTime) {
//		this.lastCapureTime = lastCapureTime;
//	}

	@Deprecated
	public Build ref() {
		Player player = owner();
		return player != null ? player.getBuildings().getBuild(id) : null;
	}
	
	public BuildV2 ref2() {
		Player player = owner();
		return player != null ? player.getBuildings2().find(id) : null;
	}
	
	public boolean removeEmployee(long eid) {
		boolean removed = false;
		Player player = vm.getOwner();
//		//---------在remove之前算产出钱
//		int buildFactor = 100;
//		BuildTemplateV2 buildTemp = App.getApp().getServiceManager().get(TemplateService.class).get(BuildTemplateV2.class, id);
//		if(buildTemp != null){
//			buildFactor = buildTemp.getProduceMoneyFactor();
//		}
//		int[] moneyInfo = this.calcMoney(buildFactor, BuildV2.MAX_TAKE_MONEY_SECOND);
//		MailAttach ma = new MailAttach();
//		ma.setGold(moneyInfo[0]);
//		App.getApp().getServiceManager().get(MailService.class).sendSystemMail(player.getId(), "撤回员工获得现金", "撤回员工获得了一些现金", ma);
//		//------------
		boolean empty = true; // 防守员工是否全空
		for (int i = employees != null ? employees.length - 1 : -1; i >= 0; i--) {
			long id = employees[i];
			if (id != 0) {
				if (id == eid) {
					employees[i] = 0;
					removed = true;
				} else {
					empty = false;
				}
			}
		}
		if (empty) {
//			ref().unCapture(player, Cause.EMPLOYEE_REMOVED); // TODO Deprecated
			ref2().unCapture(player, Cause.EMPLOYEE_REMOVED, false);
		}else{
			tryStartMakeMoney();
		}
		return removed;
	}
	
	/**
	 * 如果有人开始产钱
	 */
	public void tryStartMakeMoney(){
		lastStartMakeMoneyTime = -1;
		if(employees != null){
//			if(employees.length == 4){
//			}
			if(employees.length > 0){
				lastStartMakeMoneyTime = TimeUpdater.getInstance().now();
			}
		}
	}
	
	/**
	 * 计算钱 不结钱
	 * @param buildFactor 建筑加成百分点  100=100%
	 * @param maxSecond 
	 * @return {当前钱, 最大钱, 可领钱数}
	 */
	public int[] calcMoney(int buildFactor, int maxSecond){
		int[] ret = new int[]{0, 0, 0};
		if(lastStartMakeMoneyTime <= 0){
			return ret;
		}
		long elapseMS = TimeUpdater.getInstance().now() - this.lastStartMakeMoneyTime;
		if(elapseMS < 0){
			return ret;
		}
		Map<EmployeeQuality, EmployeeProduceMoneyByQualityV2Template> quality2Temp = App.getApp().getServiceManager().get(EmployeeService.class)
				.configure().getCommonConfigure().getQuality2produceMoneyTemplate();
		int addMoneyPerMinute = 0;
		long[] eids = employees;
		Player player = vm.getOwner();
		for(int i = 0; i < eids.length; i++){
			Employee e = player.getEmployees().get(eids[i]);
			if(e != null){
				EmployeeQuality quality = e.getTemplate().getQuality();
				EmployeeProduceMoneyByQualityV2Template temp = quality2Temp.get(quality);
				if (temp != null) {
					addMoneyPerMinute = addMoneyPerMinute + temp.getProduceMoney();
				}
			}
		}
		int addMoney = (int)(elapseMS / 1000L * addMoneyPerMinute * buildFactor / 100L / 60L);
		int maxMoney = addMoneyPerMinute * maxSecond / 60 * buildFactor / 100;
		if(addMoney > maxMoney){
			addMoney = maxMoney;
		}
		int takeMoney = (int) (addMoneyPerMinute * buildFactor / 100L * BuildV2.TAKE_MONEY_CD_SECOND / 60);
		ret[0] = addMoney;//当前钱
		ret[1] = maxMoney;//最大钱
		ret[2] = takeMoney;//产量(钱/分钟)*间隔时间(分钟)=可以领钱的钱数
//		//重置时间
//		lastStartMakeMoneyTime = TimeUpdater.getInstance().now();
//		lastTakeMoneyTime = TimeUpdater.getInstance().now();
		return ret;
	}
	
	public void clear() {
		lastStartMakeMoneyTime = -1;
		Player player = vm.getOwner();
		for (int i = employees != null ? employees.length - 1 : -1; i >= 0; i--) {
			long eid = employees[i];
			if (eid == 0L)
				continue;
			employees[i] = 0L;
			Employee e = player.getEmployees().get(eid);
			if (e != null) {
				e.setState(State.FREE, true);
			}
		}
	}
	
}
